import express from "express";
import { Remindersmodel, staffmode, depmodel } from "../db/MIne/mine.js";
import multiparty from'multiparty'
const router = express.Router();

router.post('/upload', (req, res) => {
  let form = new multiparty.Form()
  form.uploadDir = 'upload'
  form.parse(req, (err,fromdata,fildata) => {
      if (err) {
       console.log(err); 
    }
    let imgurl='http://127.0.0.1:3000/'+fildata.file[0].path
    res.send({
      code: 200,
      imgurl
    })
  })
})
router.post("/add", (req, res) => {
  Remindersmodel.create(req.body);
  res.send({
    code: 200,
    msg: "添加成功",
  });
});

router.post("/adddep", (req, res) => {
  depmodel.create(req.body);
  res.send({
    code: 200,
    msg: "添加成功",
  });
});

router.post("/addstaff", (req, res) => {
  staffmode .create(req.body);
  res.send({
    code: 200,
    msg: "添加成功",
  });
});

router.get("/getdata", async (req, res) => {
  let { page, pagesize } = req.query;
  let data = await Remindersmodel.find()
    .skip((page - 1) * pagesize)
    .limit(pagesize);
  let tatol = await Remindersmodel.find().countDocuments();
  res.send({
    code: 200,
    data,
    tatol
  });
});

router.get('/getdep', async(req, res) => {
  let data = await depmodel.find()
  res.send({
    code: 200,
    data
  })
})

router.get('/getstaff', async (req, res) => {
    
  
  let { did,page, pagesize,x,name  } = req.query
  let arr = [{}]
  if (did) arr.push({ did: did })
  if (x!='') arr.push({ sd: true })
  if (name!='') arr.push({ name: RegExp(name) })
  console.log(page, pagesize,'uigiug');
  let data = await staffmode.find({$and:arr}).skip((page - 1) * pagesize).limit(pagesize).populate('did');
    let tatol = await staffmode.find().countDocuments();
  res.send({
    code: 200,
    data,tatol
  })
})
router.delete("/del", async (req, res) => {
  let { ids } = req.query;
  await Remindersmodel.deleteMany({ _id: ids });
  res.send({
    code: 200,
    msg: "删除成功",
  });
});

router.put("/update", async (req, res) => {
  let { ids } = req.body;
  await Remindersmodel.updateMany({ _id: ids }, { status: true });
  res.send({
    code: 200,
    msg: "修改成功",
  });
});

export default router;
